﻿//*****************************************************************************
//** Project:PatternWell.PAC
//** Copyright 2011 PatternWell LLC
//** Revision History:
//** Version 1.0 - Initial Release
//*****************************************************************************
using System;
using System.Runtime.Serialization;
namespace PatternWell.PAC
{
    
    /// <summary>
    /// Base class that implements the IDisposal process recommended for PAC objects.
    /// </summary>
    [DataContract(Namespace = GlobalInfo.PacNameSpace)]
    public abstract class DisposalBase:IDisposable
    {
        #region Dispose Implementation - Base implementation of the disposal process do not modify

        /// <summary>
        /// Flag that determines if the class has been disposed.
        /// </summary>
        public bool IsDisposed { get; private set; }


        /// <summary>
        /// Releases all resources implemented in this class. Making the class avaliable for garbage collection.
        /// </summary>
        /// <remarks>
        /// Note this is not a thread safe operation. All exceptions will be swallowed during the disposal process. 
        /// If exceptions are raised during the dispose process the disposed flag will not bet set.
        /// </remarks>
        public void Dispose()
        {
            try
            {
                Dispose(IsDisposed);
                IsDisposed = true;
            }
            catch (Exception)
            {
                //Swalloing exceptions during the dispose process.
            }

        }

        /// <summary>
        /// Helper method that releases resources for this class implementation.
        /// </summary>
        /// <param name="disposed">flag that determines if the class has already been disposed.</param>
        protected virtual void Dispose(bool disposed)
        {
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
